package com.xuecheng.content.service.iml;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.CourseTecherService;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseTecherDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.base.execption.XueChengPlusException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CourseTecherServiceIml  extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTecherService {

    private final CourseTeacherMapper courseTeacherMapper;
    /**
     * 查询教师信息
     * @param id
     * @return
     */
    @Override
    public List<CourseTeacher> queryCourseTecher(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(wrapper);
        return courseTeachers;

    }

    /**
     * 新增教师信息
     * @param courseTecherDto
     */
    @Override
    public CourseTeacher saveCourseTecher(CourseTecherDto courseTecherDto) {
        CourseTeacher courseTeacher = BeanUtil.copyProperties(courseTecherDto, CourseTeacher.class);
        courseTeacher.setCreateDate(LocalDateTime.now());
        int i = courseTeacherMapper.insert(courseTeacher);
        if(i<=0){
            throw new XueChengPlusException("新增教师信息失败");
        }
        return courseTeacher;
    }

    /**
     * 修改教师信息
     * @param courseTecherDto
     * @return
     */
    @Override
    public CourseTeacher updateCourseTecher(CourseTecherDto courseTecherDto) {
        CourseTeacher courseTeacher = BeanUtil.copyProperties(courseTecherDto, CourseTeacher.class);
        int i = courseTeacherMapper.updateById(courseTeacher);
        if(i<=0){
            throw new XueChengPlusException("修改教师信息失败");
        }
        return courseTeacher;
    }

    /**
     * 删除教师信息
     * @param courseId
     * @param id
     */
    @Override
    public void deleteCourseTecher(Long courseId, Long id) {
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId,courseId)
                .eq(CourseTeacher::getId,id);
        int i = courseTeacherMapper.delete(wrapper);
        if(i<=0){
            throw new XueChengPlusException("删除教师失败");
        }
    }
}
